package ru.jexp.rcp.supply.beans.d18;

import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;

import ru.jexp.mvc.HandlerMap;
import ru.jexp.mvc.IListener;
import ru.jexp.rcp.supply.mui.JExpSupplyMessages;
import ru.jexp.rcp.supply.swtext.JExpDialogAdapter;
import ru.jexp.rcp.supply.swtext.Lookup;

public abstract class LookupView<ColumnT, ViewerT extends ColumnViewer> extends JExpDialogAdapter implements Lookup.IView
{
	private String title;
	private IObservableValue chooseEnabled;

	protected HandlerMap handlers = new HandlerMap();

	public LookupView(String title, Shell parentShell)
	{
		super(parentShell);
		this.title = title;
	}

	@Override
	protected String createTitle()
	{
		return title;
	}

	@Override
	public IObservableValue getChooseEnabled()
	{
		return chooseEnabled;
	}

	@Override
	public void addChooseHandler(IListener listener)
	{
		handlers.put(CHOOSE_EVENT, listener);
	}

	@Override
	protected void okPressed()
	{
		setReturnCode(OK);
		handlers.handle(CHOOSE_EVENT);
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent)
	{
		Button f_choose = createButton(parent, IDialogConstants.OK_ID, JExpSupplyMessages.Global_button_choose, true);
		createButton(parent, IDialogConstants.CANCEL_ID, JExpSupplyMessages.Global_button_cancel, false);

		chooseEnabled = SWTObservables.observeEnabled(f_choose);
	}
	
	@Override
	protected Rectangle getConstrainedShellBounds(Rectangle preferredSize)
	{
		Rectangle bounds = super.getConstrainedShellBounds(preferredSize);
		bounds.height += 200; 
		bounds.y -= 50; 
		return bounds;
	}
}
